package com.ks.BusAlarm;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.speech.tts.TextToSpeech;

public class PreferenceUI extends PreferenceActivity {
	Preferences pref;

	SharedPreferences sp;
	SharedPreferences.Editor spe;
	Boolean PrefVibration;
	Boolean PrefSound;
	int PrefTimer;

	CheckBoxPreference checkPrefVibration;
	CheckBoxPreference checkPrefSound;
	EditTextPreference editPrefTimer;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		sp = getSharedPreferences(getString(R.string.PREF_NAME), 0);
		spe = sp.edit();

		addPreferencesFromResource(R.layout.preferenceui);

		checkPrefVibration = (CheckBoxPreference) findPreference("prefVibration");

		PrefVibration = sp
				.getBoolean(getString(R.string.PREF_VIBRATION), false);
		checkPrefVibration.setChecked(PrefVibration);

		checkPrefVibration
				.setOnPreferenceClickListener(new OnPreferenceClickListener() {

					@Override
					public boolean onPreferenceClick(Preference preference) {

						spe.putBoolean(getString(R.string.PREF_VIBRATION),
								!PrefVibration);
						spe.commit();

						return false;
					}
				});

		checkPrefSound = (CheckBoxPreference) findPreference("prefSound");
		PrefSound = sp.getBoolean(getString(R.string.PREF_SOUND), false);
		checkPrefSound.setChecked(PrefSound);

		checkPrefSound
				.setOnPreferenceClickListener(new OnPreferenceClickListener() {

					@Override
					public boolean onPreferenceClick(Preference preference) {

						if (!PrefSound) {
							startTTSActivity();
						} else {

							spe.putBoolean(getString(R.string.PREF_SOUND),
									!PrefSound);
							spe.commit();
						}

						return false;
					}
				});

		editPrefTimer = (EditTextPreference) findPreference("editPrefTimer");
		PrefTimer = sp.getInt(getString(R.string.PREF_TIMER), 20);

		editPrefTimer.setDefaultValue((int) PrefTimer);
		ksLog.d("PrefTimer", PrefTimer);
		editPrefTimer
				.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

					@Override
					public boolean onPreferenceChange(Preference preference,
							Object newValue) {
						spe.putInt(getString(R.string.PREF_TIMER),
								Integer.parseInt(newValue.toString()));
						spe.commit();
						ksLog.d("PrefTimer", PrefTimer);

						return true;
					}
				});
	}

	private static final int MY_DATA_CHECK_CODE = 4321;

	void startTTSActivity() {

		Intent checkIntent = new Intent();
		checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);

		startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == MY_DATA_CHECK_CODE) {
			if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {

				spe.putBoolean(getString(R.string.PREF_SOUND), !PrefSound);
				spe.commit();
				ksLog.d("ok");

			} else {

				Intent installIntent = new Intent();
				installIntent
						.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
				startActivity(installIntent);
			}
		}
	}

}
